package com.ist.smartvehicle;

import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Binder;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;
import android.widget.Toast;

public class LocationService extends Service implements LocationListener{
	
	private static final String LOGTAG = "LOCATION_SERVICE";
	
	private Location currentLocation;

	public synchronized Location getLocation() {
		return currentLocation;
	}
	
	public synchronized void setLocation(Location location) {
		currentLocation = location;
	}
	
	String sayHello() {
		return "Hello";
	}
	
	@Override
	public void onCreate() {
		super.onCreate();
		Log.d(LOGTAG, "Service created...");
		Toast.makeText(this, "Service created...", Toast.LENGTH_LONG).show();
		LocationManager locationManager = (LocationManager) 
				getSystemService(Context.LOCATION_SERVICE);
		locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 
				0,0, this);
		currentLocation = locationManager.getLastKnownLocation(
				LocationManager.GPS_PROVIDER);
	}

	@Override
	public void onLocationChanged(Location location) {
		String text = new String();
		text = "User is at: [latitude = " + location.getLatitude()
				+ ", longitude = " + location.getLongitude() + ", altitude = "
				+ location.getAltitude() + "].";
		Log.d(LOGTAG, text);
		Toast.makeText(this, text, Toast.LENGTH_LONG).show();
		setLocation(location);
		
	}
	
	@Override
	public void onDestroy() {
		super.onDestroy();
		Log.d(LOGTAG, "Service destroyed...");
		Toast.makeText(this, "Service destroyed...", Toast.LENGTH_LONG).show();
	}

	@Override
	public void onProviderDisabled(String provider) { }

	@Override
	public void onProviderEnabled(String provider) { }

	@Override
	public void onStatusChanged(String provider, int status, Bundle extras) { }

	@Override
	public IBinder onBind(Intent intent) {
		return mBinder;
	}
	
    public class LocalBinder extends Binder {
        LocationService getService() {
            return LocationService.this;
        }
    }
    
    private final IBinder mBinder = new LocalBinder();
	
}
